package come.demo.widgets.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import come.demo.widgets.R;
import tzy.recyclerview.HeaderFooterRecyclerView;
import tzy.recyclerview.RecyclerView2;


public class HeaderFooterActivity extends AppCompatActivity implements View.OnClickListener {

    private HeaderFooterRecyclerView mRecyclerView;
    int mHeaderIndex = -1;
    int mFooterIndex = -1;
    private ExpandAdapter mExpandAdapter;
    static final int EXPAND_START = 4;
    static final int EXPAND_COUNT = 6;

    public static void gotoActivity(Context context) {
        Intent intent = new Intent(context, HeaderFooterActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_header_footer);
        mRecyclerView = findViewById(R.id.header_footer_recyclerview);
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        findViewById(R.id.header_add).setOnClickListener(this);
        findViewById(R.id.header_remove).setOnClickListener(this);
        findViewById(R.id.footer_add).setOnClickListener(this);
        findViewById(R.id.footer_remove).setOnClickListener(this);
//        findViewById(R.id.banner_add).setOnClickListener(this);
        findViewById(R.id.notify).setOnClickListener(this);
        mExpandAdapter = new ExpandAdapter();
        mRecyclerView.setAdapter(mExpandAdapter);
        mExpandAdapter.refreshExpand(EXPAND_START, EXPAND_COUNT, 14);
        mRecyclerView.expandLayout(false);
        final StringBuilder rangeString = new StringBuilder().append(EXPAND_START).append('~').append(EXPAND_START + EXPAND_COUNT - 1);
        ((Button) findViewById(R.id.expand)).setText(mRecyclerView.isExpanded() ? new StringBuilder().append("隐藏").append(rangeString) : new StringBuilder().append("展开").append(rangeString));

        mRecyclerView.setOnItemClickListener(new RecyclerView2.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position, long id) {
                Toast.makeText(parent.getContext(), String.valueOf(position) + "," + id, Toast.LENGTH_SHORT).show();

            }
        });
        findViewById(R.id.expand).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecyclerView.expandLayout(!mRecyclerView.isExpanded());
                ((Button) v).setText(mRecyclerView.isExpanded() ? new StringBuilder().append("隐藏").append(rangeString) : new StringBuilder().append("展开").append(rangeString));
            }
        });

    }

    private static class ExpandAdapter extends HeaderFooterRecyclerView.Adapter {
        private int expandStart;
        private int expandCount;
        private int dataSize;

        void refreshExpand(int expandStart, int expandCount, int dataSize) {
            this.expandStart = expandStart;
            this.expandCount = expandCount;
            this.dataSize = dataSize;
            notifyDataSetChanged();
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            TextView textView = new TextView(parent.getContext());
//                textView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new RecyclerView.ViewHolder(textView) {
                @Override
                public String toString() {
                    return super.toString();
                }
            };
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getTotalSpanSize() {
            return 6;
        }

        @Override
        public int getSpanSize(int position) {
            if (position >= getExpandStart() && position < getExpandStart() + getExpandCount()) {
                return 2;
            }

            return 3;
        }

        @Override
        public int getExpandStart() {
            return expandStart;
        }

        @Override
        public int getExpandCount() {
            return expandCount;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((TextView) holder.itemView).setText(String.valueOf(position));
        }

        @Override
        public int getItemCount() {
            return dataSize;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.header_add:
                ++mHeaderIndex;
                final TextView header = new TextView(this);
                header.setText("我是第" + mHeaderIndex + "个headerView");
                mRecyclerView.addHeaderView(mHeaderIndex, header);
                break;
            case R.id.header_remove:
                if (mHeaderIndex >= 0 && mRecyclerView.removeHeaderView(mHeaderIndex)) {
                    --mHeaderIndex;
                } else {
                    Toast.makeText(this, "删除HeaderView失败", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.footer_add:
                ++mFooterIndex;
                final TextView footer = new TextView(this);
                footer.setText("我是第" + mFooterIndex + "个footerView");
                mRecyclerView.addFooterView(mFooterIndex, footer);
                break;
            case R.id.footer_remove:
                if (mFooterIndex >= 0 && mRecyclerView.removeFooterView(mFooterIndex)) {
                    --mFooterIndex;
                } else {
                    Toast.makeText(this, "删除FooterView失败", Toast.LENGTH_SHORT).show();
                }

                break;
//            case R.id.banner_add:
//
//                try {
//                    final MyViewPager pager = new MyViewPager(this);
//                    pager.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (200 * getResources().getDisplayMetrics().density)));
//                    Integer[] ress = new Integer[]{R.mipmap.banner0, R.mipmap.banner1};
//                    pager.setAdapter(new HomeBannerAdapter(ress));
//                    mRecyclerView.addHeaderView(0, pager);
//                } catch (IllegalStateException e) {
//                    Toast.makeText(this, "banner已经添加!!", Toast.LENGTH_SHORT).show();
//                }
//
//                break;
            case R.id.notify:
                mExpandAdapter.refreshExpand(4, 6, 14);
                break;
        }
    }


}
